package org.snail.common.xmlBean;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class XmlCommon {
        private static Map<String , String> map = new HashMap<String, String>();

        public static Map parseXml(String filePath) throws DocumentException{
            Map<String , String> mapEle = new HashMap<String, String>();
            //StringBuffer sb = new StringBuffer();
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(filePath));//读取xml文档
            Element root = document.getRootElement();//得到xml文档根节点元素，即最上层的"<xml>"
//            System.out.println("输出根节点元素的名字===" + root.getName());
            //循环遍历出更节点下面的子节点
            List<Element> elementList = root.elements();
            Iterator<Element> it = elementList.iterator();
            while (it.hasNext()){
                Element element = it.next();
//                 System.out.println(element);
                mapEle = printEle(element);
            }
            return mapEle;
        }
        public static int i = 0;

        public static Map printEle(Element ele){
            //StringBuffer sb = new StringBuffer();
            if(ele.elements().size()==0){
                if (i == 0){
                    System.out.println("该节点没有子节点，打印出节点名称" + ele.getName() +"打印出节点的值" + ele.getText() + "============" + i);
                    map.put(ele.getName() , ele.getText());
                } else if (i == 1) {
                    if (ele.getParent().getName().equals("level")) {

                    }else {
                        map.put(ele.getName() , ele.getText());
                    }
                    System.out.println("该节点有子节点，父节点名称："+ ele.getParent().getName() +"打印出节点名称" + ele.getName() +"打印出节点的值" + ele.getText() + "============" + i);
                }
            }
            else{
                i = 1;
                List<Element> elementList = ele.elements();
                Iterator<Element> it = elementList.iterator();
                while(it.hasNext()){
                    printEle(it.next());
                }
            }
            return map;
        }

        /**
         * @param args
         * 功能测试
         */
        public static void main(String[] args) {
            Map<String , String> mapEleList = new HashMap<String, String>();
            //StringBuffer sb = new StringBuffer();
            try {
                String filePath = XmlToDocHolder.class.getClassLoader().getResource("src/DingGroup.xml").getPath();
                mapEleList = XmlCommon.parseXml(filePath);
                //遍历map
                Iterator<Map.Entry<String, String>> it = mapEleList.entrySet().iterator();
                while(it.hasNext()){
                    Map.Entry<String , String> entry = it.next();
                    System.out.println(entry.getKey() +"-------"+entry.getValue());
                }
            //System.out.println(sb.toString());
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
}
